package cn.service.impl;

import cn.dao.UsersDao;
import cn.entity.Users;
import cn.service.UsersService;
import cn.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UsersServiceImpl implements UsersService{
	@Autowired
	UsersDao usersDao;
	@Override
	public int deleteByPrimaryKey(Object id) {
		return usersDao.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Users record) {
		return usersDao.insert(record);
	}

	@Override
	public Users selectByPrimaryKey(Object id) {
		return usersDao.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKey(Users record) {
		return usersDao.updateByPrimaryKey(record);
	}

	@Override
	public List<Users> find(String name, PageBean pageBean) {
		List<Users> list=new ArrayList<Users>();
		try{
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("name", name);
			if(pageBean!=null){
				params.put("begin", (pageBean.getPageNo()-1)*pageBean.getPageSize());
				params.put("pageSize", pageBean.getPageSize());
			}
			list=usersDao.find(params);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return list;
	}

	@Override
	public int getCount(String name) {
		int n=0;
		try{
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("name", name);
			n=usersDao.getCount(params);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return n;
	}

	@Override
	public Users login(String username, String password,Object role) {
		Users users=null;
		try{
			List<Users> list=usersDao.login(username,password,role);
			if(list!=null && list.size()>0){
				users=list.get(0);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return users;
	}
	
}
